from scrapy.pipelines.images import ImagesPipeline
from scrapy import Request
# 图片存储管道

class ImageSaveSpiderPipeline(ImagesPipeline):
    # 重写图片下载的管道方法，进行图片下载
    def get_media_requests(self, item, info):
       img_url = item['title_photo_url']
       yield Request(url=img_url, meta={'name': item['title'], 'item_id': item['item_id']})


    # 重命名图片的路径及图片标题
    def file_path(self, request, response=None, info=None):
        name = request.meta['name']
        item_id = str(request.meta['item_id'])
        return '%s/%s.jpg' % (name, item_id)